package com.spider.controller;

import java.io.IOException;
import java.io.PrintWriter;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.spider.servlet.Person;

@Controller 
public class PersonControll { 
	private static Logger log = Logger.getLogger(PersonControll.class);
    @RequestMapping("toAjax.do") 
    public String toAjax() { 
        return "ajax"; 
    } 
       
    @RequestMapping(value = "ajax.do", method = RequestMethod.GET) 
    public void ajax(@ModelAttribute Person person,PrintWriter printWriter) { 
    	log.info(person); 
        String jsonString =""; //doRequest();//JSON.toJSONString(person, SerializerFeature.PrettyFormat);
        try {

			HttpClient httpclient = new DefaultHttpClient();
                        //register https protocol in httpclient's scheme registry
			/* 建立HTTP Post连线 */
			HttpGet httpPost = new HttpGet("http://completion.amazon.co.uk/search/complete?method=completion&mkt=4&client=amazon-search-ui&x=String&search-alias="+person.getAge()+"&q="+person.getName()+"&qs=&cf=1&noCacheIE=1433745547165&fb=1&sc=1&");
			httpPost.setHeader("Accept","application/javascript, */*;q=0.8");
			httpPost.setHeader("Referer","http://www.amazon.de/dp/B00KAKPZYG/ref=br_imp_ara-1/275-1217088-6758352?_encoding=UTF8&nav_sdd=aps%2Fref%3Dgw_aftv_187642527_1&pf_rd_m=A3JWKAKR8XB7XF&pf_rd_s=desktop-hero-kindle-A&pf_rd_r=1WQ1BC28XD70Z3T5C8GH&pf_rd_t=36701&pf_rd_p=633168907&pf_rd_i=desktop");
			httpPost.setHeader("Accept-Language","zh-CN");
			httpPost.setHeader("User-Agent","Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)");
			httpPost.setHeader("Accept-Encoding","gzip, deflate");
			httpPost.setHeader("Host","completion.amazon.co.uk");
			httpPost.setHeader("Connection","Keep-Alive");
			log.info("REQUEST:" + httpPost.getURI());
			HttpResponse httpResponse = httpclient.execute(httpPost);
			if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				jsonString= EntityUtils.toString(httpResponse.getEntity());
				jsonString=jsonString.substring(13,jsonString.length()-11);
				log.info( jsonString);
			} else {
				log.info("doGet Error Response: " + httpResponse.getStatusLine().toString());
				
			}
			
		}  catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception ex) {
			ex.printStackTrace();

		}
        printWriter.write(jsonString); 
        printWriter.flush(); 
        printWriter.close(); 
    } 
    
}